package org.jeecg.modules.rooms.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.rooms.DTO.CesAllDayPriceRuleDto;
import org.jeecg.modules.rooms.Enum.CouponEnums;
import org.jeecg.modules.rooms.entity.CesAllDayPriceRule;
import org.jeecg.modules.rooms.entity.CesAllDayRuleMinute;
import org.jeecg.modules.rooms.mapper.CesAllDayPriceRuleMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 全天房计费规则表 服务实现类
 * </p>
 *
 * @author Claude
 * @since 2023-03-07
 */
@Service
public class CesAllDayPriceRuleServiceImpl extends ServiceImpl<CesAllDayPriceRuleMapper, CesAllDayPriceRule> implements IService<CesAllDayPriceRule> {

    @Resource
    private CesAllDayPriceRuleMapper cesAllDayPriceRuleMapper;
    @Resource
    private CesAllDayRuleMinuteServiceImpl cesAllDayRuleMinuteService;

    /**
     * 修改全天房设置
     * @param cesAllDayPriceRuleDto
     * @return
     */
    @Transactional(rollbackOn = Exception.class)
    public Result modifyWholeDayUnifyPrice(CesAllDayPriceRuleDto cesAllDayPriceRuleDto) {
        CesAllDayPriceRule cesAllDayPriceRule = cesAllDayPriceRuleMapper.selectById(cesAllDayPriceRuleDto.getId());
        if(ObjectUtils.isEmpty(cesAllDayPriceRule)){
            cesAllDayPriceRule =  new CesAllDayPriceRule();
        }
        BeanUtil.copyProperties(cesAllDayPriceRuleDto,cesAllDayPriceRule, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        saveOrUpdate(cesAllDayPriceRule);
        //分钟加收
        if (CouponEnums.ALLDAYROOMRULETIMEOUT.MINUTE.code() == cesAllDayPriceRuleDto.getTimeOutRule().intValue()){
            //按房型设置下
            if(CouponEnums.ALLDAYROOMRULETYPEDETAILS.LAYOUT.code() == cesAllDayPriceRuleDto.getRuleType().intValue()){
                //按房型规则插入
                List<CesAllDayRuleMinute> roomLayoutRules = cesAllDayPriceRuleDto.getRoomLayoutRules();
                roomLayoutRules.forEach(v -> {
                    v.setHotelId(cesAllDayPriceRuleDto.getHotelId());
                    v.setUpdateAt(LocalDateTime.now());
                    if(StringUtils.isEmpty(v.getId())){
                        v.setCreateAt(LocalDateTime.now());
                    }
                });
                cesAllDayRuleMinuteService.saveOrUpdateBatch(roomLayoutRules);
            }
        }
        return Result.ok("成功!");
    }

    public CesAllDayPriceRule defaultRule() {
        CesAllDayPriceRule d = new CesAllDayPriceRule();
        d.setEndTime("14:00");
        d.setLeaveTime("12:00");
        d.setDayTime("10");
        d.setMinute(10);
        d.setMoreThenMinute(30);
        d.setMoreThenPrice(new BigDecimal(3));
        d.setPrice(new BigDecimal(5));
        d.setRuleType(1);
        d.setTimeOutRule(1);
        return d;
    }


    /**
     * 查询
     * @param hotelId
     * @return
     */
    public Result fetch(String hotelId){
        Map<String,Object> map =new HashedMap();
        CesAllDayPriceRule cesAllDayPriceRule =  cesAllDayPriceRuleMapper.selectOne(Wrappers.<CesAllDayPriceRule>lambdaQuery().eq(CesAllDayPriceRule::getHotelId,hotelId).eq(CesAllDayPriceRule::getInvalid,false));
        if(ObjectUtils.isEmpty(cesAllDayPriceRule)) return Result.ok(null);

        //分钟加收
//        if (CouponEnums.ALLDAYROOMRULETIMEOUT.MINUTE.code() == cesAllDayPriceRule.getTimeOutRule().intValue()){
//            //按房型设置下
////            if(CouponEnums.ALLDAYROOMRULETYPEDETAILS.LAYOUT.code() == cesAllDayPriceRule.getRuleType().intValue()){
////
////            }
//
//        }
        List<CesAllDayRuleMinute> cesAllDayRuleMinutes =  cesAllDayRuleMinuteService.getRuleMinutesByHotelId(hotelId);
        map.put("cesAllMinutes",cesAllDayRuleMinutes);
        map.put("cesAllDayPriceRule",cesAllDayPriceRule);
        return Result.ok(map);
    }




}
